#! /usr/bin/python

import os
import json

ohos_root = r"/home/cyd/ohos"

def get_part_paths():
    cmd = 'find -name bundle.json -not -path "./out/*"'

    re = os.popen(cmd).readlines()
    part_paths = []
    for i in re:
        bundle_file = open(i.strip('\n'), 'rb')
        bundle_json = json.load(bundle_file)
        if 'segment' in bundle_json: # bundle 编写不规范
            if 'destPath' in bundle_json['segment']:
                path = bundle_json['segment']['destPath']
                part_paths.append(path)
            else:
                print('Warning: no destPath: ' + i)
        else:
            print('Warning: no segment: ' + i)
        bundle_file.close()
    return part_paths 

def output_to_file(l):
    f = open('part_list.txt', 'w')
    for i in l:
        if type(i) is list: # bundle 编写不规范
            print('Warning: too many path: ' + str(i))
        else:
            f.write(i + '\n')
    f.close()

if __name__ == "__main__":
    cur_path = os.getcwd()
    os.chdir(ohos_root)
    l = get_part_paths()
    os.chdir(cur_path)
    output_to_file(l)